{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 远程推理应用示例3\n",
    "\n",
    "本示例使用 Deeplabv3 模型远程推理，进行图像语义分割处理，以图片方式显示结果，简要说明了基于Atlas 200DK 开发板的远程推理应用开发方法之一。\n",
    "\n",
    "## 主要特点：\n",
    "\n",
    "1. 使用 Inferemote.DataSource 类获取输入，输入源可为图片目录、视频文件、摄像头或者图片的HTTP超链接地址；\n",
    "2. 构造 Deeplabv3 模型算法对象，及其应用的回调函数；\n",
    "3. 使用 Inferemote.testing 包，进行远程推理并对结果进行展示，展示方式可为图像窗口或WEB页面。\n",
    "\n",
    "## 远程推理平台\n",
    "\n",
    "本示例与所使用的远程推理集群服务器均运行在华为云ECS服务器上，集群中的12台Atlas200DK开发板部署于电子科技大学校园. 具体请参考 https://gitee.com/haojiash/atlas200dk_remote.\n",
    "\n",
    "## 代码示例\n",
    "【提醒】：注意修改 RemoteIP 和 测试输入 设置。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 导入软件包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Inferemote: a Remote Inference Toolkit for Ascend 310\n",
    "\n",
    "\"\"\"\n",
    "import os, sys\n",
    "\n",
    "from inferemote.testing.data_source import ImageFactory #ImageSource, VideoSource, UrlSource\n",
    "from inferemote.testing import QuickTest, LivewebTest"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 实例化语义分割应用算法的对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from inferemote.airlab import Deeplabv3\n",
    "air = Deeplabv3()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 语义分割应用的回调函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "''' 对语义分割结果进行进一步处理，将得到的mask合并在原图像中 '''\n",
    "def test_func(image):\n",
    "    result = air.inference_remote(image)\n",
    "    image = air.make_mask(image, result)\n",
    "    return image"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. 远程推理设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "03-18 17:28:03 [INFO]  -atlas- Trying model inference on REMOTE ``192.168.1.123''...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tcp_remote.TcpRemote at 0x7f995a6ebf70>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''' 设置远程模型推理服务地址，以下为华为云ECS上的集群服务器： '''\n",
    "remote = '192.168.1.123'\n",
    "\n",
    "''' 关联远程推理端 '''\n",
    "air.use_remote(remote)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5. 设置输入图像源，可以为：摄像头、视频文件、图片目录或者图片的HTTP超链接地址 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以URL图片为输入\n",
    "url_input = 'https://c7xcode.obs.cn-north-4.myhuaweicloud.com/models/deeplabv3/cat.jpg'\n",
    "\n",
    "# 以图片目录为输入\n",
    "dir_input = './data/pictures'\n",
    "\n",
    "# 以摄像头为输入（如服务在ECS云服务器中，实际不具有摄像头）\n",
    "camear_input = 'camera'\n",
    "\n",
    "# 以视频文件为输入\n",
    "video_input = './data/skiing.mp4'\n",
    "\n",
    "# 设置用于本次测试的输入源\n",
    "test_input = url_input\n",
    "\n",
    "# 自动获取输入的Image流\n",
    "ds = ImageFactory().get_stream(dir_input)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6. 运行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "03-18 17:28:06 [WARNING]  --------- Data source CLOSED -----------\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "(Press CTRL+C to quit)\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "03-18 17:28:07 [INFO]  -atlas- Time costs: 0.012/0.658/0.007(s) and 0.677(s) for pre/remote/post and totally.\n",
      "03-18 17:28:07 [INFO]  - job - fps: 1.34, 0.749s ellapsed for [ 1/1 ] frames.\n",
      "03-18 17:28:07 [INFO]  - job - DONE: 0.751 seconds running for 1 frames toatally with fps: 1.332.\n",
      "03-18 17:28:07 [INFO]  ======== Exited. ========\n"
     ]
    }
   ],
   "source": [
    "''' 构造QuickTest对象，进行远程推理。\n",
    "注意：1. QuickTest 在Jupyter中不能使用 opencv显示；2. LivewebTest 在jupyter中出现端口冲突无法使用 '''\n",
    "\n",
    "test = QuickTest(ds, output='.', threads=4)\n",
    "\n",
    "''' 开始测试 '''\n",
    "test.run(test_func)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ends."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
